package com.apabi.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.apabi.common.enums.UserStatusEnum;
import com.apabi.common.enums.YesOrNo;
import com.apabi.common.utils.IDUtils;
import com.apabi.mapper.SysUserMapper;
import com.apabi.pojo.SysUser;
import com.apabi.pojo.SysUserExample;
import com.apabi.pojo.SysUserExample.Criteria;
import com.apabi.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private SysUserMapper userMapper;

	@Autowired
	private Sid sid;


	@Override
	public void saveUser(SysUser user) {

		String userId = sid.nextShort();
		user.setId(userId);
		userMapper.insert(user);
	}


	@Override
	public SysUser queryUserInfoById(String userId) {
		SysUser user = userMapper.selectByPrimaryKey(userId);
		return user;
	}

	@Override
	public void deleteUserById(String userId) {
		// userMapper.deleteByPrimaryKey(userId);

		SysUser user = new SysUser();
		user.setId(userId);
		user.setIsDelete(YesOrNo.YES.value);
		userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public void updateUserById(SysUser user) {
		userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public boolean queryNicknameIsExist(String nickname, String userId) {

		SysUserExample userExample = new SysUserExample();
		Criteria userCriteria = userExample.createCriteria();
		userCriteria.andNicknameEqualTo(nickname);

		if (StringUtils.isNotEmpty(userId)) {
			userCriteria.andIdNotEqualTo(userId);
		}

		int counts = userMapper.countByExample(userExample);
		return counts > 0;
	}

	@Override
	public boolean queryUsernameIsExist(String username, String userId) {

		SysUserExample userExample = new SysUserExample();
		Criteria userCriteria = userExample.createCriteria();
		userCriteria.andUsernameEqualTo(username);

		if (StringUtils.isNotEmpty(userId)) {
			userCriteria.andIdNotEqualTo(userId);
		}

		int counts = userMapper.countByExample(userExample);
		return counts > 0;
	}

	@Override
	public SysUser findByUsername(String username) {
		return userMapper.selectByUserName(username);
	}

	@Override
	public void saveRegistEmail(SysUser sysUser) {
		String uuid = IDUtils.genUUID();
		String userId = sid.nextShort();
		sysUser.setId(userId);
		sysUser.setActiveCode(uuid);
		sysUser.setRegistTime(new Date());
		sysUser.setIsDelete(UserStatusEnum.NOT_ACTIVE.value);
		userMapper.insert(sysUser);
	}

	@Override
	public void updateActiveEmail(String validateCode, String email) throws Exception {
		SysUser user = userMapper.selectByUserName(email);
		if (user != null) {
			// 验证用户激活状态
			if (user.getIsDelete().equals(new Integer(UserStatusEnum.NOT_ACTIVE.value))) {
				/// 没激活
				Date currentTime = new Date();// 获取当前时间
				// 验证链接是否过期
				// currentTime.before(user.getRegistTime());
				if (currentTime.before(DateUtils.addHours(user.getRegistTime(), 48))) {
					// 验证激活码是否正确
					if (validateCode.equals(user.getActiveCode())) {
						// 激活成功， //并更新用户的激活状态，为已激活
						user.setIsDelete(UserStatusEnum.ACTIVATED.value);// 把状态改为激活
						userMapper.updateByPrimaryKey(user);
					} else {
						throw new Exception("激活码不正确");
					}
				} else {
					throw new Exception("激活码已过期！");
				}
			} else {
				throw new Exception("邮箱已激活，请登录！");
			}
		} else {
			throw new Exception("该邮箱未注册（邮箱地址不存在）！");
		}

	}

	@Override
	public void resetPwd(SysUser sysUser) {
		SysUser user = userMapper.selectByUserName(sysUser.getUsername());
		String uuid = IDUtils.genUUID();
		user.setActiveCode(uuid);
		userMapper.updateByPrimaryKey(user);

	}

	@Override
	public void updateRestPwd(String validateCode, String email) throws Exception {
		SysUser user = userMapper.selectByUserName(email);
		Date currentTime = new Date();// 获取当前时间
		// 验证链接是否过期
		// currentTime.before(user.getRegistTime());
		if (currentTime.before(DateUtils.addHours(user.getRegistTime(), 48))) {
			// 验证激活码是否正确
			if (validateCode.equals(user.getActiveCode())) {
				userMapper.updateByPrimaryKey(user);
			} else {
				throw new Exception("激活码不正确");
			}
		} else {
			throw new Exception("激活码已过期！");
		}

	}
}
